<html ng-app="app">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="../../../build/common/base.css" rel="stylesheet">
<script type="text/javascript" src="../../../build/common/base.js"></script>
<script type="text/javascript" src="../../../build/common/ngEdit.js"></script>

<script type="text/javascript">
	var app = angular.module('app', [ 'baseDirective' ]);
	app.controller("ctrl", [ '$scope', 'baseService', 'ArrayToolService', function($scope, baseService, ArrayToolService) {
		$scope.ArrayTool = ArrayToolService;

		$scope.init = function() {
			$scope.boCode = window.passData.parentScope.data.boKey;
			$scope.data = {};
			$scope.data.paramMap = {};
			$scope.data.initParamMap = {};
			if (window.passData.data) {
				$scope.data = window.passData.data;
				$scope.dialogKeyChange();
			}
			//获取bo信息
			var url = __ctx + "/bus/businessObject/getObject";
			var defer = baseService.postForm(url, {
				key : $scope.boCode,
				fill : true
			});
			$.getResultData(defer, function(data) {
				$scope.bo = data;
				$scope.columnList = [];//字段数组
				$scope.handlePath(data.relation);
				$scope.findDateColumn(data.relation);
			}, null, true);
		};

		$scope.$watch("data.dialogKey", function(newValue, oldValue) {
			if (newValue && newValue == oldValue) {
				return;
			}
			$scope.dialogKeyChange();
		});

		$scope.dialogKeyChange = function() {
			//获取对话框数据
			var url = __ctx + "/form/formCustDialog/getObject";
			var defer = baseService.postForm(url, {
				key : $scope.data.dialogKey
			});
			$.getResultData(defer, function(data) {
				$scope.dialog = data;
			});
		};

		//计算出bo的path
		$scope.handlePath = function(relation) {
			if (relation.type == "main") {
				relation.path = "data." + $scope.bo.key;
			}
			if (relation.type == "oneToMany") {
				relation.path = relation.tableKey;
			}
			if (relation.type == "oneToOne") {
				relation.path = relation.parent.path + "." + relation.tableKey;
			}
			angular.forEach(relation.children, function(child) {
				$scope.handlePath(child);
			});
		}

		//计算下拉框的值
		$scope.findDateColumn = function(relation) {
			angular.forEach(relation.table.columnsWithOutHidden, function(column) {
				var path = relation.path + "." + column.key;
				$scope.columnList.push({
					desc : column.comment + "(" + column.table.comment + ")",
					path : path,
				});
			});
			//递归子关系
			angular.forEach(relation.children, function(child) {
				$scope.findDateColumn(child);
			});
		};
	} ]);

	//父页面打开按【确定】按钮时的回调函数
	function getData() {
		var scope = AngularUtil.getScope();
		var json = {};
		angular.forEach(scope.data.paramMap, function(val, key) {
			if (val) {
				json[key] = val;
			}
		});
		scope.data.paramMap = json;

		json = {};
		angular.forEach(scope.data.initParamMap, function(val, key) {
			if (val) {
				json[key] = val;
			}
		});
		scope.data.initParamMap = json;

		return scope.data;
	}
</script>
</head>
<body ng-controller="ctrl" ng-init="init()">
	<form name="form">
		<table class="form-table">
			<tr>
				<th>自定义查询名字</th>
				<td>
					<input ng-show="dialog.key" type="text" class="form-control" ng-model="dialog.name" style="width: 220px">
					<span ng-show="!dialog.key" class="text-warning">请选择一个自定义对话框</span>
					<a class="btn btn-outline btn-primary fa-edit" value-key="data.dialogKey" ab-cust-dialog="zdydhklb" href="javascript:void(0)" dialog-setting="{multiple:false}">选择 </a>
				</td>
				<th>对话框别名</th>
				<td>{{dialog.key}}</td>
			</tr>
		</table>
		<div ng-show="dialog">
			<div style="font-size: 20px; text-align: center;">自定义查询动态入参设置</div>
			<table class="form-table">
				<tr>
					<th>查询入参</th>
					<th>参数来源</th>
					<th>默认值</th>
				</tr>
				<tr ng-repeat="field in dialog.conditionFields">
					<td>{{field.showName}}（{{field.columnName}}）</td>
					<td>
						<select class="form-control" ng-model="data.paramMap[field.columnName]" ng-options="m.path as m.desc for m in columnList">
							<option value="">请选择</option>
						</select>
					</td>
					<td>
						<input type="text" class="form-control" ng-model="data.initParamMap[field.columnName]" />
					</td>
				</tr>
			</table>
		</div>

		<div ng-show="dialog">
			<div style="font-size: 20px; text-align: center;">下拉框Option配置</div>
			<table class="form-table">
				<tr>
					<th>option值（value）</th>
					<td>
						<select class="form-control" ng-model="data.opVal" ng-options="m.returnName as m.returnName for m in dialog.returnFields">
							<option value="">请选择</option>
						</select>
					</td>
				</tr>
				<tr>
					<th>option文本（text）</th>
					<td>
						<select class="form-control" ng-model="data.opTxt" ng-options="m.returnName as m.returnName for m in dialog.returnFields">
							<option value="">请选择</option>
						</select>
					</td>
				</tr>
			</table>
		</div>
	</form>
</body>
</html>